﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPVisualDev
{
    internal partial class DialogResourceNewCulture : Form
    {
        private List<CultureInfo> m_list_of_cultures;

        public DialogResourceNewCulture(List<CultureInfo> ListOfCultures)
        {
            InitializeComponent();

            m_list_of_cultures = ListOfCultures;
        }

        public string InfoLabel
        {
            get;
            set;
        }


        public string SelectedCulture
        {
            get;
            private set;
        }

        public string AcceptButtonText
        {
            set { btnAdd.Text = value; }
        }

        private struct CultureInfoItem
        {
            public string Value { get; set; }
            public string Text { get; set;}
        }

        private void DialogResourceNewCulture_Load(object sender, EventArgs e)
        {
            //CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

            cboCultures.Sorted = true;
            cboCultures.ValueMember = "Value";
            cboCultures.DisplayMember = "Text";

            if (m_list_of_cultures != null)
            {
                foreach (CultureInfo c in m_list_of_cultures)
                {
                    if (c.LCID != CultureInfo.InvariantCulture.LCID)
                    {
                        CultureInfoItem item = new CultureInfoItem();

                        item.Text = c.Name + " " + c.DisplayName + " LCID: " + c.LCID.ToString() + "";
                        item.Value = c.Name;

                        cboCultures.Items.Add(item);
                    }
                }
            }

            if (cboCultures.Items.Count > 0)
                cboCultures.SelectedIndex = 0;
            else
            {
                cboCultures.Text = "No cultures to select";
                btnAdd.Enabled = false;
            }

            lblInfo.Text = InfoLabel;
            this.Text = InfoLabel;

            this.FormClosing += new FormClosingEventHandler(DialogResourceNewCulture_FormClosing);
        }

        private void DialogResourceNewCulture_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.DialogResult == DialogResult.OK)
                {
                    if (cboCultures.SelectedIndex == -1)
                    {
                        string msg = "No culture is selected.";
                        MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        e.Cancel = true;
                    }
                    else
                    {
                        CultureInfoItem item = (CultureInfoItem)cboCultures.SelectedItem;
                        SelectedCulture = item.Value;
                    }
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

    }
}
